﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SmlEngine.Graphics
{
    /// <summary>
    /// Represents an animated texture.
    /// </summary>
    /// <remarks>
    /// The number of frames in the animation are inferred from 
    /// the FrameWidth, which must be explicitly specified.
    /// </remarks>
    public class Animation
    {
        /// <summary>
        /// All frames in the animation arranged horizontally.
        /// </summary>
        public Texture2D Texture { get; private set; }

        /// <summary>
        /// Duration of time to show each frame.
        /// </summary>
        public float FrameTime
        {
            get { return frameTime; }
        }
        float frameTime;

        /// <summary>
        /// When the end of the animation is reached, should it
        /// continue playing from the beginning?
        /// </summary>
        public bool IsLooping
        {
            get { return isLooping; }
        }
        bool isLooping;

        /// <summary>
        /// Gets the number of frames in the animation.
        /// </summary>
        public int FrameCount
        {
            get { return Texture.Width / FrameWidth; }
        }

        /// <summary>
        /// Gets the width of a frame in the animation.
        /// </summary>
        public int FrameWidth { get; set; }

        /// <summary>
        /// Gets the height of a frame in the animation.
        /// </summary>
        public int FrameHeight
        {
            get { return Texture.Height; }
        }

        /// <summary>
        /// Constructs a new animation.
        /// </summary>        
        public Animation(Texture2D texture, float frameTime, int frameCount, bool isLooping)
        {
            Texture = texture;
            this.frameTime = frameTime;
            this.isLooping = isLooping;
            this.FrameWidth = texture.Width / frameCount;
        }

        /// <summary>
        /// Constructs a new animation from the specified line of a Texture2D
        /// </summary>
        /// <param name="texture">The source Texture2D</param>
        /// <param name="frameTime">The time to show the frame for.</param>
        /// <param name="frameCount">The numberof frames in the animation.</param>
        /// <param name="isLooping">If the animation loops.</param>
        /// <param name="lineNumber">The line of the Texture2D to retrieve the animation from.</param>
        /// <param name="height">The height of the animation.</param>
        public Animation(Texture2D texture, float frameTime, int frameCount, bool isLooping, int lineNumber, int height)
        {
            this.frameTime = frameTime;
            this.isLooping = isLooping;
            this.FrameWidth = texture.Width / frameCount;
            Texture = GraphicsHelper.Crop(texture, new Rectangle(0, height * lineNumber, texture.Width, height));
        }
    }
}
